package cn.m3a1.task;

import cn.m3a1.dao.UserDao;
import cn.m3a1.model.*;
import com.iceking.dynamicweb.FrmkView;
import com.iceking.dynamicweb.GlobalUtils;
import com.iceking.dynamicweb.Pagination;
import com.iceking.dynamicweb.TaskProcessor;
import net.sf.json.JSONObject;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wyj
 * @date 2019/6/6
 * @time 下午2:00
 */
public class HeadTask  extends  TaskProcessor{
    private User loginUser;
    public Boolean panduan(){
        loginUser = (User) getSession().getAttribute("loginUser");
        if (loginUser==null){
            return false;
        }

        int type=loginUser.getType();

        if (type!=70){
            return false;
        }
        return  true;
    }
    /**
     * 查询班主任所带班级下学生的信息
     * @throws Exception
     */
    public void findstu() throws Exception{

        FrmkView frmkView=new FrmkView("headstu.html");
        int classid = getParamInt("classid");
        getDBConn().startTransaction();
        String sql = "select  `user`.id,`user`.`code`,user.`name`,`user`.tel,`user`.email from  `user`,studentofclass" +
                " where `user`.id=studentofclass.studentid and studentofclass.classid="+classid;
        ResultSet rs = getDBConn().query(sql);
        List<User> list = new ArrayList<>();
        while (rs.next()) {
            //Sc sc=new Sc();
            User user=new User();

            user.setId(rs.getInt("id"));
            user.setCode(rs.getString("code"));
            user.setName(rs.getString("name"));
            user.setTel(rs.getString("tel"));
            user.setEmail(rs.getString("email"));
            list.add(user);
        }
        rs.close();
        getDBConn().stopTransaction();
        frmkView.addData("members",list);
        toFreemarker(frmkView);
    }

    /**
     * 查询所有请假信息
     * @throws Exception
     */
    public void findleave() throws Exception{
        if (!panduan()){
            return;
        }
        UserDao userDao=new UserDao();


        if (loginUser.getType()!=70){
            return;
        }
        int status = getParamInt("status");
        int type=loginUser.getType();
        FrmkView frmkView=new FrmkView("headleave.html");
        //查询所有班级信息
        getDBConn().startTransaction();
        String sql = "select `user`.`code`,`user`.`name`,absentnote.* from `user`,absentnote where `user`.id=absentnote.studentid and absentnote.managerid="+loginUser.getId();
        if (status==1){
            sql+=" and managercfm=1";
        }else if (status==2){
            sql+=" and managercfm=2";
        }else if (status==3){
            sql+=" and managercfm=0";
        }
        ResultSet rs = getDBConn().query(sql);
        List<Userabsent> list = new ArrayList<>();
        while (rs.next()) {
            //Sc sc=new Sc();
            Userabsent user=new Userabsent();
            user.setId(rs.getInt("id"));
            user.setCode(rs.getString("code"));
            user.setStuname(rs.getString("name"));
            user.setClassid(rs.getInt("classid"));
            user.setExtfile(rs.getString("extfile"));
            user.setManagerid(loginUser.getId());
            user.setReason(rs.getString("reason"));
            user.setTimestamp(rs.getLong("timestamp"));
            user.setPeriod(rs.getString("period"));
            user.setManagercfm(rs.getInt("managercfm"));
            user.setTeachercfm(rs.getInt("teachercfm"));
            list.add(user);
        }
        rs.close();
        getDBConn().stopTransaction();
        frmkView.addData("loginUser",loginUser);
        frmkView.addData("absentnote",list);
        toFreemarker(frmkView);
    }

    /**
     * 班主任审核意见
     *
     * @throws Exception
     */
    public void update() throws Exception {
        if (!panduan()){
            return;
        }

        if (loginUser.getType()!=70){
            return;
        }
        int id = getParamInt("id");
        int type = getParamInt("type");
        String managermark = getParam("managermark");
        JSONObject json = new JSONObject();
        Absentnote absentnote=new Absentnote();

        Pagination<Absentnote> pg = new Pagination<>();
        pg.appendWhere("where id = ? ", id);
        pg.appendWhere("and managerid = ? ", loginUser.getId());
        getDBConn().select(Absentnote.class, pg);

        absentnote = pg.getFirstResult();
        if (type==1&&absentnote.getManagercfm()==0){
            absentnote.setManagercfm(1);
            absentnote.setManagermark(managermark);
            if (absentnote.getCourseid()==0){
                absentnote.setStatus(2);
            }else{
                absentnote.setStatus(1);
            }

            getDBConn().save(absentnote);

        }else if(type==2&&absentnote.getManagercfm()==0){
            absentnote.setManagercfm(2);
            absentnote.setManagermark(managermark);
            absentnote.setStatus(2);
            getDBConn().save(absentnote);


        }else{
            json.put("succ", false);
            json.put("stmt", "无法更改");
            toJson(json.toString());
        }
        //记录日志
        Syslog syslog=new Syslog();
        syslog.setWho("班主任，"+","+loginUser.getUsername());
        syslog.setWhat("审批请假");
        syslog.setWhattime(System.currentTimeMillis());
        getDBConn().save(syslog);
        json.put("succ", true);
        json.put("stmt", "更改成功");
        toJson(json.toString());


    }


}
